package test.my.com.myapplicationtest.wave_view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import test.my.com.myapplicationtest.utils.DensityUtil;

/**
 * Created by ah on 2017/4/8.
 */

public class WaveSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{

    // 波纹颜色
    private static final int WAVE_PAINT_COLOR = 0xb3ffffff;
    private static final int TRANSLATE_X_SPEED = 1;
    private float mCycleFactorW;

    private int mTotalWidth, mTotalHeight;
    private float[] mYPositionsSinOne;
    private float[] mYPositionsSinTwo;
    private float[] mYPositionsCos;
    private float[] mResetOneYPositions;
    private float[] mResetTwoYPositions;
    private float[] mResetThreeYPositions;
    private int mXOffsetSpeedOne;
    private int mXOneOffset;

    private Paint mWavePaint;

    private Thread mThread;
    private SurfaceHolder surfaceHolder;
    private Canvas mCanvas;
    private LinearGradient mLinearGradient;
    private Rect mRect;
    private boolean isReady;

    public WaveSurfaceView(Context context) {
        super(context);
        initView(context);
    }

    public WaveSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public WaveSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        // 将dp转化为px，用于控制不同分辨率上移动速度基本一致
        mXOffsetSpeedOne = DensityUtil.dip2px(context, TRANSLATE_X_SPEED);

        // 初始绘制波纹的画笔
        mWavePaint = new Paint();
        mWavePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        // 去除画笔锯齿
        mWavePaint.setAntiAlias(true);
        // 设置风格为实线
        mWavePaint.setStyle(Paint.Style.FILL);
        // 设置画笔颜色
        mWavePaint.setColor(WAVE_PAINT_COLOR);
//        mWavePaint.setShader(mLinearGradient);

        surfaceHolder = this.getHolder();
        surfaceHolder.addCallback(this);
        setZOrderOnTop(true);
        surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);

    }

    private void drawWave() {
        mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        for (int i = 0; i < mTotalWidth; i++) {

            // 减400只是为了控制波纹绘制的y的在屏幕的位置，大家可以改成一个变量，然后动态改变这个变量，从而形成波纹上升下降效果
            // 绘制第一条水波纹
            mCanvas.drawLine(i, mTotalHeight - mResetOneYPositions[i] - mTotalHeight / 2, i,
                    mTotalHeight,
                    mWavePaint);

            // 绘制第二条水波纹
            mCanvas.drawLine(i, mTotalHeight - mResetTwoYPositions[i] - mTotalHeight / 2, i,
                    mTotalHeight,
                    mWavePaint);
//
////            // 绘制第三条水波纹
            mCanvas.drawLine(i, mTotalHeight - mResetThreeYPositions[i] - mTotalHeight / 2, i,
                    mTotalHeight,
                    mWavePaint);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 记录下view的宽高
        mTotalWidth = w;
        mTotalHeight = h;
        // 用于保存原始波纹的y值 sin
        mYPositionsSinOne = new float[mTotalWidth];
        // 用于保存原始波纹的y值 sin
        mYPositionsSinTwo = new float[mTotalWidth];
        // 用于保存原始波纹的y值 cos
        mYPositionsCos = new float[mTotalWidth];
        // 用于保存波纹一的y值
        mResetOneYPositions = new float[mTotalWidth];
        // 用于保存波纹二的y值
        mResetTwoYPositions = new float[mTotalWidth];
        // 用于保存波纹三的y值
        mResetThreeYPositions = new float[mTotalWidth];

        // 将周期定为view总宽度
        mCycleFactorW = (float) (2 * Math.PI / mTotalWidth);

        // 根据view总宽度得出所有对应的y值
        for (int i = 0; i < mTotalWidth; i++) {
            mYPositionsSinOne[i] = (float) (40 * Math.sin(mCycleFactorW * i)/* + OFFSET_Y*/);
//            mYPositionsSinOne[i] = (float) (Math.sin(mCycleFactorW * i)/* + OFFSET_Y*/);
        }

//        // 根据view总宽度得出所有对应的y值
        for (int i = 0; i < mTotalWidth; i++) {
            mYPositionsSinTwo[i] = (float) (40 * Math.sin(mCycleFactorW * i - Math.PI) /*+ OFFSET_Y*/);
        }
//
//        // 根据view总宽度得出所有对应的y值
        for (int i = 0; i < mTotalWidth; i++) {
            mYPositionsCos[i] = (float) (40 * Math.cos(mCycleFactorW * i) /*+ OFFSET_Y*/);
        }
    }

    private void resetPositonY() {
        // mXOneOffset代表当前第一条水波纹要移动的距离
        int yOneInterval = mYPositionsSinOne.length - mXOneOffset;
        // 使用System.arraycopy方式重新填充第一条波纹的数据
        System.arraycopy(mYPositionsSinOne, mXOneOffset, mResetOneYPositions, 0, yOneInterval);
        System.arraycopy(mYPositionsSinOne, 0, mResetOneYPositions, yOneInterval, mXOneOffset);

        int yTwoInterval = mYPositionsSinTwo.length - mXOneOffset;
        System.arraycopy(mYPositionsSinTwo, mXOneOffset, mResetTwoYPositions, 0,
                yTwoInterval);
        System.arraycopy(mYPositionsSinTwo, 0, mResetTwoYPositions, yTwoInterval, mXOneOffset);

        int ythreeInterval = mYPositionsCos.length - mXOneOffset;
        System.arraycopy(mYPositionsCos, mXOneOffset, mResetThreeYPositions, 0,
                ythreeInterval);
        System.arraycopy(mYPositionsCos, 0, mResetThreeYPositions, ythreeInterval, mXOneOffset);
    }

    public void initWave(boolean isWave) {
//        if (isWave) {
//            setZOrderMediaOverlay(true);
//        } else {
//            setZOrderMediaOverlay(false);
//        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isReady = true;
        mThread = new Thread(this);
        mThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isReady = false;
    }

    @Override
    public void run() {
        while (isReady) {
            resetPositonY();
            mXOneOffset += mXOffsetSpeedOne;
//        // 如果已经移动到结尾处，则重头记录
            if (mXOneOffset >= mTotalWidth) {
                mXOneOffset = 0;
            }

            try {
//                synchronized (surfaceHolder) {
//                }
                mCanvas = surfaceHolder.lockCanvas();
                if (mCanvas != null) {
                    drawWave();
                }
                surfaceHolder.unlockCanvasAndPost(mCanvas);
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            } /*finally {
                if (surfaceHolder != null && mCanvas != null) {
                    surfaceHolder.unlockCanvasAndPost(mCanvas);
                }
            }*/

        }
    }
}
